#include "vxlabelscontainer.h"

VxLabelsContainer::VxLabelsContainer(QWidget * parent) :
		QFrame(parent)
{
	_layout = new QHBoxLayout();

	this->setLayout(_layout);

	this->setObjectName("vxLabelsContainer");

	_menu.setObjectName("vxLabelMenu");
	_menu.setFocusPolicy(Qt::NoFocus);

	_addButton = new QPushButton();
	_layout->insertWidget(0, _addButton);
	_addButton->setObjectName("vxAddLabelButton");
	_addButton->setFocusPolicy(Qt::NoFocus);

	connect(_addButton, SIGNAL(clicked()), this, SLOT(_addLabelsMenu()));
}


void VxLabelsContainer::_addLabelsMenu()
{
	_execMenu(_addButton, false);
}


VxLabel * VxLabelsContainer::addLabel(VxLabelData * data, QWidget * container)
{
	VxLabel * label = new VxLabel(data);
	this->layout()->addWidget(label);
	_labels.insert(label);
	label->getLabel()->installEventFilter(this);

	_layout->removeWidget(_addButton);
	if (_layout->count() > 0){
		_layout->insertWidget(_layout->count(), _addButton);
	} else {
		_layout->addWidget(_addButton);
	}

	_checkAddButton();

	return label;
}


void VxLabelsContainer::_checkAddButton()
{
	layout()->removeWidget(_addButton);
	layout()->addWidget(_addButton);
	_addButton->setVisible(_haveMenuItems());
}


bool VxLabelsContainer::eventFilter(QObject * obj, QEvent * event)
{

	switch(event->type()){

	case QEvent::MouseButtonPress: {
			if (obj->parent()->inherits("VxLabel")){
				_execMenu(static_cast<QWidget *>(obj->parent()), true);
			}
		}

	default:;

	}

	return QWidget::eventFilter(obj, event);
}


bool VxLabelsContainer::_haveMenuItems()
{
	foreach(VxLabel * label, _labels){
		if (label->isHidden()){
			return true;
		}
	}
	return false;
}



class ChangeLabelHelper : public QObject
{

	Q_OBJECT

public:
	ChangeLabelHelper(VxLabel * label, VxLabel * labelTo) :
			QObject(), _label(label), _labelTo(labelTo)
	{

	}


public slots:
	void changeLabel()
	{
		QBoxLayout * layout = static_cast<QBoxLayout *>(_label->parentWidget()->layout());

		int ix = layout->indexOf(_label);

		layout->insertWidget(ix, _labelTo);
		_label->hide();
		_labelTo->show();

	}

	void destroy()
	{
		delete this;
	}

private:
	VxLabel * _label;
	VxLabel * _labelTo;

};



void VxLabelsContainer::_execMenu(QWidget * widget, bool changeAction)
{

	_menu.clear();

	if (changeAction){
		VxLabel * label = static_cast<VxLabel *>(widget);
		foreach(VxLabel * labelTo, _labels){
			if (labelTo->isHidden()){
				ChangeLabelHelper * clhelper = new ChangeLabelHelper(label, labelTo);
				QAction * addAction = new QAction(labelTo->getData()->getCaption(), this);
				connect(addAction, SIGNAL(triggered()), clhelper, SLOT(changeLabel()));
				connect(addAction, SIGNAL(destroyed()), clhelper, SLOT(destroy()));
				connect(addAction, SIGNAL(triggered()), this, SLOT(_checkAddButton()));
				_menu.addAction(addAction);
			}
		}
	} else {
		foreach(VxLabel * label, _labels){
			if (label->isHidden()){
				QAction * addAction = new QAction(label->getData()->getCaption(), this);
				connect(addAction, SIGNAL(triggered()), label, SLOT(showLabel()));
				connect(addAction, SIGNAL(triggered()), this, SLOT(_checkAddButton()));
				_menu.addAction(addAction);
			}
		}
	}


	if (changeAction){
		if (_menu.actions().count() > 0){
			_menu.addSeparator();
		}

		QAction * removeAction = new QAction(tr("remove"), widget);
		_menu.addAction(removeAction);
		connect(removeAction, SIGNAL(triggered()), widget, SLOT(hideLabel()));
		connect(removeAction, SIGNAL(triggered()), this, SLOT(_checkAddButton()));

	}

	_checkAddButton();

	if (_menu.actions().count() > 0){
		QPoint p = widget->mapToGlobal(widget->contentsRect().bottomLeft());
		p.setY(p.y()-1);

		_menu.exec(p);
	}

}


#include "vxlabelscontainer.moc"
